﻿'/**********************************************************************
'/// <summary>
'// Name of File     :   Notes.vb
'// Author           :   Sukhminder Singh
'// Create date      :   25th Feb 2011
'/// </summary>
'************************************************************************/
Imports Crystal.CLL
Imports CrystalDataHub.BLL
Imports System.Text
Imports CrystalDataHub.BLL.DataHub.BusinessObject

Public Class Notes
    Inherits BaseForm

#Region "Fields"
    Public menmScreenType As DataHubScreenType
    Public mintProjectID As Integer
    Public mstrInvestorFundIDs As String
    Public mblnIsSavedSuccess As Boolean = False
    Public mintCurrentInvestorID As Integer
    Public mintCurrentLinkID As Integer
    Public mstrNotes As String
    Public mstrDENotesDefaultText As String
    Public mstrFLNotesDefaultText As String
    Public mstNotesStatus As IList(Of NotesStatus)

    Private mclsDataHubManager As New DataHubManager
    Private mblnIsDEClicked As Boolean = False
    Private mblnIsFLClicked As Boolean = False
    Private mstrFormName As String = "Notes"
    Public mstCurrentDataSources As IList(Of CurrentDataSources)
#End Region

#Region "On Load"

    Private Sub Notes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CallOnLoad()
    End Sub

    ''' <summary>
    ''' To load the onload functions
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub CallOnLoad()
        SetOnLoadControls()
    End Sub

    ''' <summary>
    ''' Set the controls on load
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetOnLoadControls()
        Me.MaximizeBox = False
        Me.Text = TitleManager.GetTitle("Notes")
        rdDEUpend.Checked = True
        rdFLUpend.Checked = True
        txtDENotes.Text = mstrDENotesDefaultText
        txtFLNotes.Text = mstrFLNotesDefaultText
        If menmScreenType = DataHubScreenType.DataEntry Then
            SetDEControls(True)
            SetFLControls(False)
            chkDE.Checked = True
            chkFL.Enabled = False
        ElseIf menmScreenType = DataHubScreenType.FinalizeList Then
            SetFLControls(True)
            SetDEControls(False)
            chkFL.Checked = True
            chkDE.Checked = False
        End If
    End Sub

#End Region

#Region "Events"

#Region "Form Events"
    Private Sub Notes_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub
#End Region

#Region "Checkbox Events"

    Private Sub chkDE_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles chkDE.MouseClick
        SetDEControls(chkDE.Checked)
    End Sub

    Private Sub chkFL_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles chkFL.MouseClick
        SetFLControls(chkFL.Checked)
    End Sub

#End Region

#Region "Text Box Events"
    Private Sub txtDENotes_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDENotes.GotFocus
        If mblnIsDEClicked = False Then
            SetNotesText(txtDENotes)
            mblnIsDEClicked = True
        End If
    End Sub

    Private Sub txtFLNotes_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFLNotes.GotFocus
        If mblnIsFLClicked = False Then
            SetNotesText(txtFLNotes)
            mblnIsFLClicked = True
        End If
    End Sub
#End Region

#Region "Button Events"
    ''' <summary>
    ''' To save the Notes data into the database
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim enmResult As DBResult = DBResult.SUCCESS
        Dim lstrDENotes As String = ""
        Dim lstrFLNotes As String = ""
        Dim lenmDENotesSaveType As DataHubManager.NotesSaveType
        Dim lenmFLNotesSaveType As DataHubManager.NotesSaveType
        Dim lstrLockedBy As New StringBuilder
        Dim lblnHasAnyLockedCol As Boolean = False
        For Each lobjCurrentDataSources As CurrentDataSources In mstCurrentDataSources
            If lobjCurrentDataSources.AccessStatus = SourceAccessbility.Locked.ToString Then
                If lobjCurrentDataSources.SourceTypeCode = SourceTypeCode.UPD.ToString Then
                    lstrLockedBy.Append(lobjCurrentDataSources.SourceName.Substring(0, lobjCurrentDataSources.SourceName.Length - lobjCurrentDataSources.ListingName.Length).Trim)
                    Exit For
                Else
                    lstrLockedBy.Append(lobjCurrentDataSources.SourceName)
                End If
                lblnHasAnyLockedCol = True
            End If
        Next
        If lstrLockedBy.Length > 0 Then
            lstrLockedBy.Append(" - ")
        End If

        'Check the type of save i.e. Append OR AsItIs
        If rdDEUpend.Checked Then
            lenmDENotesSaveType = DataHubManager.NotesSaveType.Append
        Else
            lenmDENotesSaveType = DataHubManager.NotesSaveType.AsItIs
        End If

        If rdFLUpend.Checked Then
            lenmFLNotesSaveType = DataHubManager.NotesSaveType.Append
        Else
            lenmFLNotesSaveType = DataHubManager.NotesSaveType.AsItIs
        End If

        'Get the data to be saved if the checkbox is ticked
        If chkDE.Checked Then
            If lenmDENotesSaveType = DataHubManager.NotesSaveType.Append Then
                If txtDENotes.Text.Trim.StartsWith(mstrDENotesDefaultText) Then
                    lstrDENotes = Environment.NewLine + txtDENotes.Text.Trim.Replace(mstrDENotesDefaultText, "")
                End If
            Else
                lstrDENotes = txtDENotes.Text.Trim
            End If
        End If

        If chkFL.Checked Then
            If lenmFLNotesSaveType = DataHubManager.NotesSaveType.Append Then
                If txtFLNotes.Text.Trim.StartsWith(mstrFLNotesDefaultText) Then
                    lstrFLNotes = Environment.NewLine + txtFLNotes.Text.Trim.Replace(mstrFLNotesDefaultText, "")
                End If
            Else
                lstrFLNotes = txtFLNotes.Text.Trim
            End If
        End If
        Dim lintUserNameTextLength As Integer = ("- " + CommonCLL.UserFullName + ":").Length
        Dim lstrEnteredText As String
        Dim lintCompareResults As Int16

        'Check if the DENotes is blank i.e It just contains the Name of the current user
        'If lstrDENotes.Length > lintUserNameTextLength Then
        '    If String.Compare(("- " + CommonCLL.UserFullName + ":"), lstrDENotes.Substring(lstrDENotes.Length - lintUserNameTextLength, lintUserNameTextLength)) = 0 Then
        '        lstrDENotes = lstrDENotes.Substring(0, lstrDENotes.Length - lintUserNameTextLength)
        '        lstrDENotes = lstrDENotes.Trim
        '    End If
        'End If
        If lstrDENotes.Length > lintUserNameTextLength Then
            If lblnHasAnyLockedCol Then
                lstrEnteredText = mstrDENotesDefaultText + "- " + lstrLockedBy.ToString + CommonCLL.UserFullName + " :".Trim
                lintCompareResults = String.Compare(lstrDENotes, lstrEnteredText)
                If lintCompareResults = 0 Then
                    lstrDENotes = mstrDENotesDefaultText.Trim
                Else
                    If lstrDENotes.EndsWith("- " + lstrLockedBy.ToString + CommonCLL.UserFullName + " :".Trim) Then
                        lstrDENotes = lstrDENotes.Substring(0, lstrDENotes.Length - ("- " + lstrLockedBy.ToString + CommonCLL.UserFullName + " :".Trim).Length).Trim()
                    ElseIf lstrDENotes.EndsWith("- " + CommonCLL.UserFullName + " :".Trim) Then
                        lstrDENotes = lstrDENotes.Substring(0, lstrDENotes.Length - ("- " + CommonCLL.UserFullName + " :".Trim).Length).Trim()
                    End If
                End If
            Else
                lstrEnteredText = mstrDENotesDefaultText + CommonCLL.UserFullName + " :".Trim
                If String.Compare(lstrDENotes, lstrEnteredText) = 0 Then
                    lstrDENotes = mstrDENotesDefaultText.Trim
                End If
            End If
        End If

        'Check if the FLNotes is blank i.e It just contains the Name of the current user
        If lstrFLNotes.Length > lintUserNameTextLength Then
            If String.Compare(("- " + CommonCLL.UserFullName + ":"), lstrFLNotes.Substring(lstrFLNotes.Length - lintUserNameTextLength, lintUserNameTextLength)) = 0 Then
                lstrFLNotes = lstrFLNotes.Substring(0, lstrFLNotes.Length - lintUserNameTextLength)
                lstrFLNotes = lstrFLNotes.Trim
            End If
        End If

        'If the DENotes is blank i.e It just contains the Name of the current user OR just contains the default text then set the Notes text for DB to blank
        If lstrDENotes.Equals("") OrElse lstrDENotes.Equals(mstrDENotesDefaultText.Trim) OrElse lstrDENotes.Equals("- " + CommonCLL.UserFullName + ":") Then
            lstrDENotes = ""
        End If
        'If the FLNotes is blank i.e It just contains the Name of the current user OR just contains the default text then set the Notes text for DB to blank
        If lstrFLNotes.Equals("") OrElse lstrFLNotes.Equals(mstrFLNotesDefaultText.Trim) OrElse lstrFLNotes.Equals("- " + CommonCLL.UserFullName + ":") Then
            lstrFLNotes = ""
        End If
        'Confirm from the user if he wants to save it as blank
        If (lstrDENotes = "- " + CommonCLL.UserFullName + ":" OrElse lstrDENotes.Equals("")) AndAlso (lstrFLNotes = "- " + CommonCLL.UserFullName + ":" OrElse lstrFLNotes.Equals("")) Then
            If MessageBox.Show("Are you sure you wish to leave the DE Notes text box blank?", CommonCLL.mstrMsgBoxCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                Me.Close()
                Exit Sub
            Else
                Exit Sub
            End If
        End If
        'If the Notes are selected to be appended, then Append a new blank row
        If chkDE.Checked AndAlso lenmDENotesSaveType <> DataHubManager.NotesSaveType.Append Then
            'lstrDENotes = Environment.NewLine + lstrDENotes
        End If

        'If the Notes are selected to be appended, then Append a new blank row
        If chkFL.Checked AndAlso lenmFLNotesSaveType <> DataHubManager.NotesSaveType.Append Then
            'lstrFLNotes = Environment.NewLine + lstrFLNotes
        End If

        'Save the notes in the database
        mstNotesStatus = mclsDataHubManager.ManageNotes(mintProjectID, mstrInvestorFundIDs, lstrDENotes, lstrFLNotes, CommonCLL.UserID, enmResult, lenmDENotesSaveType, lenmFLNotesSaveType, mintCurrentInvestorID, mintCurrentLinkID, mstrNotes)
        'mclsDataHubManager.ManageNotes(mintProjectID, mstrInvestorFundIDs, lstrNotes, True, CommonCLL.UserID, enmResult, lenmNotesSaveType)
        If enmResult = DBResult.FAILED Then
            Me.ShowMessageBox(DBResult.FAILED, mstrFormName)
        ElseIf enmResult = DBResult.EXIST Then
            Me.ShowMessageBox(DBResult.EXIST, mstrFormName)
        ElseIf enmResult = DBResult.SUCCESS Then
            mblnIsSavedSuccess = True
            MessageBox.Show("Notes saved successfully.", CommonCLL.mstrMsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.Close()
        End If
    End Sub

    ''' <summary>
    ''' Closes the form
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

#End Region

#End Region

#Region "Procedures"
    ''' <summary>
    ''' Set the DE controls
    ''' </summary>
    ''' <param name="pblnStatus"></param>
    ''' <remarks></remarks>
    Private Sub SetDEControls(ByVal pblnStatus As Boolean)
        txtDENotes.Enabled = pblnStatus
        pnlDENotes.Enabled = pblnStatus
    End Sub

    ''' <summary>
    ''' Set the FL controls
    ''' </summary>
    ''' <param name="pblnStatus"></param>
    ''' <remarks></remarks>
    Private Sub SetFLControls(ByVal pblnStatus As Boolean)
        txtFLNotes.Enabled = pblnStatus
        pnlFLNotes.Enabled = pblnStatus
    End Sub

    ''' <summary>
    ''' Set the default text in Notes text box
    ''' </summary>
    ''' <param name="txtNotes"></param>
    ''' <remarks></remarks>
    Private Sub SetNotesText(ByVal txtNotes As TextBox)
        Dim lstrLockedBy As New StringBuilder
        If txtNotes Is txtDENotes Then
            For Each lobjCurrentDataSources As CurrentDataSources In mstCurrentDataSources
                If lobjCurrentDataSources.AccessStatus = SourceAccessbility.Locked.ToString Then
                    If lobjCurrentDataSources.SourceTypeCode = SourceTypeCode.UPD.ToString Then
                        lstrLockedBy.Append(lobjCurrentDataSources.SourceName.Substring(0, lobjCurrentDataSources.SourceName.Length - lobjCurrentDataSources.ListingName.Length).Trim)
                        Exit For
                    Else
                        lstrLockedBy.Append(lobjCurrentDataSources.SourceName)
                    End If
                End If
            Next
            If lstrLockedBy.Length > 0 Then
                lstrLockedBy.Append(" - ")
            End If
        Else
            lstrLockedBy.Append("")
        End If

        If txtNotes.Text.Length > CommonCLL.UserFullName.Length Then
            If CommonCLL.Reverse(CommonCLL.Reverse(txtNotes.Text.Trim).Substring(2, CommonCLL.UserFullName.Length - 1)) <> CommonCLL.UserFullName.Trim Then
                'If txtNotes.Text.Substring(txtNotes.Text.Trim.TrimEnd(":").Length - CommonCLL.UserFullName.Length, CommonCLL.UserFullName.Length).Trim() <> CommonCLL.UserFullName.Trim Then
                If txtNotes.Text.Length > 0 Then
                    If System.Text.Encoding.ASCII.GetBytes(txtNotes.Text(txtNotes.Text.Length - 1))(0) <> 10 Then
                        txtNotes.Text += Environment.NewLine
                    End If
                End If
                txtNotes.Text += "- " + lstrLockedBy.ToString + CommonCLL.UserFullName + ": "
            End If
        Else
            If txtNotes.Text.Length > 0 Then
                If System.Text.Encoding.ASCII.GetBytes(txtNotes.Text(txtNotes.Text.Length - 1))(0) <> 10 Then
                    txtNotes.Text += Environment.NewLine
                End If
            End If
            txtNotes.Text += "- " + lstrLockedBy.ToString + CommonCLL.UserFullName + ": "
        End If
        txtNotes.Select(txtNotes.Text.Length, 0)
        lstrLockedBy = Nothing
    End Sub
#End Region

End Class